package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.ActorMenuDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.MenuDto;
import com.hualude.roadsign.model.ActorMenuEntity;
import com.hualude.roadsign.repository.ActorMenuRepository;
import com.hualude.roadsign.repository.MenuRepository;
import com.hualude.roadsign.service.ActorMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu-jiaHui on 2017/4/9.
 */
@Service
public class ActorMenuServiceImpl implements ActorMenuService {

    @Resource
    private ActorMenuRepository actorMenuRepository;
    @Resource
    private MenuRepository menuRepository;

    public DTResponseDto getActorMenuByActorId(DTRequestDto dtRequestDto, Integer actorId) throws Exception {
        List<ActorMenuDto> actorMenuDtos = actorMenuRepository.getActorMenuByActorId(dtRequestDto, actorId);
        Integer count = actorMenuRepository.getActorMenuCountByActorId(actorId, dtRequestDto.getSearch());
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, actorMenuDtos);
        return dtResponseDto;
    }

    public List<MenuDto> getMenu4List() throws Exception {
        return menuRepository.getMenu4List();
    }

    @Transactional
    public Integer updateActorMenuByAmId(Integer amId, Integer powerInsert, Integer powerDelete, Integer powerUpdate,
                                         Integer powerSelect, Integer powerCheck) throws Exception {
        return actorMenuRepository.updateActorMenuByAmId(amId, powerInsert, powerDelete, powerUpdate, powerSelect, powerCheck);
    }

    @Transactional
    public Integer addMenuByActorId(Integer actorId, String menuIds) throws Exception {
        String[] menuIdArray = menuIds.split(",");
        Integer result = 0;
        int len = menuIdArray.length;
        for (int i = 0; i < len; i++) {
            Integer count = actorMenuRepository.getCountByIds(actorId, Integer.parseInt(menuIdArray[i]));
            if (count > 0)
                result = 0;
            else {
                ActorMenuEntity actorMenuEntity = new ActorMenuEntity();
                actorMenuEntity.setActorId(actorId);
                actorMenuEntity.setMenuId(Integer.parseInt(menuIdArray[i]));
                if (Integer.parseInt(menuIdArray[i]) % 100 == 0)
                    actorMenuEntity.setMenuType(1);
                else
                    actorMenuEntity.setMenuType(2);
                actorMenuEntity.setPowerCheck(1);
                actorMenuEntity.setPowerDelete(1);
                actorMenuEntity.setPowerInsert(1);
                actorMenuEntity.setPowerSelect(1);
                actorMenuEntity.setPowerUpdate(1);
                result = actorMenuRepository.save(actorMenuEntity).getAmId();
            }

        }
        return result;
    }

    @Transactional
    public Integer deleteActorMenuByAmId(Integer amId) throws Exception {
        return actorMenuRepository.deleteActorMenuByAmId(amId);
    }
}
